//
//  SexViewController.m
//  Dear
//
//  Created by Taiping002 on 15/7/13.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
//

#import "SexViewController.h"
#import "SexCell.h"
#import "DDetailViewController.h"
#import "SexModel.h"
#import "CommentModel.h"
@interface SexViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray * _dataArr;
    UITableView *_tableView;
    BOOL _isRefresh;
}
@end

@implementation SexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTitleViewWith:self.titleStr];
    //数据
    [self initData];
    [self uiConfig];

    [self getDataFromServer];
    [self createRefresh];

}
- (void)initData{
    _dataArr = [[NSMutableArray alloc]init];
}
- (void)getDataFromServer {
    
    //0 健康养生
    NSDictionary *dict = @{@"userId":[DearUser sharedInstance].userId,@"type":self.type};
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpInformation];
    [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        if (responseObject) {
            NSLog(@"%@",UUIDStr);
            if (_dataArr.count > 0) {
                [_dataArr removeAllObjects];
            }
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict[@"Result"] integerValue] == 0) {
                NSArray *itemArr = dict[@"item"];
                for (NSDictionary *itemDict in itemArr) {
                    SexModel *model = [[SexModel alloc]init];
                    [model setValuesForKeysWithDictionary:itemDict];
                    [_dataArr addObject:model];
                }
                [_tableView reloadData];
            }else {
                [DearLoadingView showTips:dict[@"Message"]];
            }
        }else{
            [DearLoadingView showTips:@"获取失败"];
        }
        [self endRefresh];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [DearLoadingView hideHUD];
        [DearLoadingView showTips:error];
    }];
}

- (void)createRefresh{
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (_isRefresh) {
            return ;
        }
        _isRefresh = YES;
        [self getDataFromServer];
    }];
    _tableView.header.lastUpdatedTimeKey = SexUpdateTime;

}
- (void)endRefresh
{
    _isRefresh = NO;
    [_tableView.header endRefreshing];
}


- (void)uiConfig {
    _tableView = [[UITableView alloc]initWithFrame:(CGRectMake(0, 0, ScreenSize.width, ScreenSize.height - 64))];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    UIButton * button = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    [button setTitle:@"查看更多" forState:(UIControlStateNormal)];
//    button.layer.masksToBounds = YES;
//    button.layer.borderWidth = 0.5;
//    button.layer.borderColor = RGBA(231, 231, 231, 1).CGColor;
//    [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
//    button.frame = CGRectMake(0, 0, ScreenSize.width, 44);
//    _tableView.tableFooterView = button;
    [self.view addSubview:_tableView];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * cellID = @"SexCell";
    SexCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"SexCell" owner:self options:nil]lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    if (_dataArr.count > 0) {
        SexModel *sexModel = _dataArr[indexPath.row];
        cell.sexModel = sexModel;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDetailViewController *dvc  = [[DDetailViewController alloc]init];
    if (_dataArr.count > 0) {
        SexModel *model = _dataArr[indexPath.row];
        dvc.model = model;
    }
    
    [self.navigationController pushViewController:dvc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
